﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class ITXPDC : DocumentBase
    {
        #region-------------Properties-----------------------

        string sql = "";
        DataTable TResult = null;
        DataRow dwMachine = null;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string ABFAGANG = "";
        string ROWID = "";
        bool IsLoaded = false;
        DataRow CurrentRow = null;
        DataSet SResult = null;
        DataRow LastRow = null;
        DataSet SetMap = null;
        public DataSet dsResult;
        MainViewModel MainViewModel;
  
        public SETINTEXDB SetData
        {
            get
            {
                return MainViewModel.SetData;
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return MainViewModel.SetProd;
            }
        }
        CollectionViewSource vwTPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTPDC");
            }
        }
 /*
        SQLiteDatabase SQLiteDatabase
        {
            get
            {
                return MainViewModel.SQLiteDatabase;
            }
        }*/
        DataTable TBResult;
        #endregion

        public ITXPDC()
        {
            this.Name = "作业采集";
            InitializeComponent(); 
            if (INTEXDBC == null)
            {
                app.Load();
            }
            MainViewModel = new MainViewModel();
            DataContext = MainViewModel;
        }

        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            if (PDCTYPE.SelectedIndex < 0)
            {
                MessageBox.Show("请先选择采集类型");
                return;
            }
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helpers.Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            txtLogIn.Background = new SolidColorBrush(Colors.Green);
            expLog.IsExpanded = false;
            expLog.Visibility = Visibility.Collapsed;
            gdMain.Visibility = Visibility.Visible;
        }

     
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (app.User.Trim() == "ITXV")
                {
                    //btnSetup.Visibility = Visibility.Visible;
                    txtUserID.Text = app.User;
                    txtPassword.Password = app.Pwd;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void PDCTYPE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            ABFAGANG = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
            switch (ABFAGANG.Trim())
            {
                case "XS":
                    vwITXPDCXS.Visibility = Visibility.Visible;
                    vwITXPDCXS.ABFAGANG = ABFAGANG;
                    vwITXPDCCS.Visibility = Visibility.Collapsed;
                    break;
                case "CS":
                    vwITXPDCCS.Visibility = Visibility.Visible;
                    vwITXPDCCS.ABFAGANG = ABFAGANG;
                    vwITXPDCXS.Visibility = Visibility.Collapsed;
                    break;
            }
        }
         

        private void BtnSetUp_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                /*
                SQLiteDatabase.Close();
                if (File.Exists(MainViewModel.dbConnection))
                {
                    File.Delete(MainViewModel.dbConnection);
                }
                if (!File.Exists(app.company + ".s3db"))
                {
                    MessageBox.Show("请检查文件" + app.company + ".s3db");
                    return;
                }
                File.Copy(app.company + ".s3db", MainViewModel.dbConnection);
                string NOrder = "1";
                DataRow dwData = SetEnv.ICC_PDC.NewRow();
                dwData["TERMNO"] = app.TERMNO;
                DataSet SResult = INTEXDBC.QueryICC_PDC(dwData, 3);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    string max = row["MAXORD"].ToString();
                    if (max.Trim() == "")
                        break;
                    NOrder =max.Replace(app.TERMNO.Trim() + ".", "");                     
                }
                sql = "UPDATE ICC_MAXORD SET MAXORD= '" + NOrder + "'";
                SQLiteDatabase.ExecuteNonQuery(sql);
                sql = "DELETE FROM ICC_PDC";
                SQLiteDatabase.ExecuteNonQuery(sql);*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("请手工移动文件");
            }
        }
    }
}
